<template>
  <div class="search-container">
    <svg
      width="22"
      height="22"
      viewBox="0 0 24 24"
      fill="none"
      xmlns="http://www.w3.org/2000/svg"
      class="search-icon"
      @click="toggleSearch"
    >
      <path
        d="M21 21L16.514 16.506L21 21ZM19 10.5C19 15.194 15.194 19 10.5 19C5.806 19 2 15.194 2 10.5C2 5.806 5.806 2 10.5 2C15.194 2 19 5.806 19 10.5Z"
        stroke="currentColor"
        stroke-width="2"
        stroke-linecap="round"
        stroke-linejoin="round"
      />
    </svg>
    <div 
      class="search-input-wrapper"
      :class="{ 'expanded': isExpanded }"
    >
      <input
        type="text"
        class="search-input"
        placeholder="请输入菜单内容"
        v-model="searchValue"
        @input="handleSearch"
        ref="searchInput"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, nextTick } from 'vue'

const searchValue = ref('')
const isExpanded = ref(false)
const searchInput = ref<HTMLInputElement>()

const toggleSearch = async () => {
  isExpanded.value = !isExpanded.value
  
  if (isExpanded.value) {
    // 等待动画完成后聚焦输入框
    await nextTick()
    setTimeout(() => {
      searchInput.value?.focus()
    }, 300)
  } else {
    // 收起时清空搜索内容
    searchValue.value = ''
  }
}

const handleSearch = () => {
  // 处理搜索逻辑
  console.log('搜索内容:', searchValue.value)
}
</script>

<style scoped>
.search-container {
  display: flex;
  align-items: center;
}

.search-icon {
  color: #666666;
  cursor: pointer;
  transition: color 0.2s ease;
  z-index: 10;
}

.search-icon:hover {
  color: #333333;
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  background-color: #f5f5f5;
  border-radius: 6px;
  padding: 6px 10px;
  border: 1px solid #e0e0e0;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
  width: 0;
  opacity: 0;
  margin-left: -10px;
  margin-right: 6px;

}

.search-input-wrapper.expanded {
  width: 280px;
  opacity: 1;
  margin-left: 8px;
}

.search-input-wrapper:hover {
  border-color: #d0d0d0;
  background-color: #f8f8f8;
}

.search-input-wrapper:focus-within {
  border-color: #d0d0d0;
  background-color: #ffffff;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
}

.search-icon-inner {
  color: #666666;
  margin-right: 8px;
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  color: #333333;
  padding: 0;
  min-width: 0;
}

.search-input::placeholder {
  color: #999999;
  font-size: 14px;
}

.search-input:focus {
  outline: none;
}
</style>
